package com.xzmzhp.controller.activity;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzmzhp.constant.ReturnConstant;
import com.xzmzhp.pojo.activity.ProgramType;
import com.xzmzhp.service.activity.ProgramTypeService;
import com.xzmzhp.utils.StringUtils;
import com.xzmzhp.utils.return_tool.ReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author: 王源(xm)
 * @create: 2022-04-04 10:53
 * @program: ams
 * @description: 节目类型前端交互类
 */
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/programType")
public class ProgramTypeController {

    @Autowired
    private ProgramTypeService programTypeService;

    /**
     * 查询所有节目类型信息
     * @return 节目类型信息
     */
    @RequestMapping("/findAll")
    public ReturnData findAll(@RequestParam(value = "page", defaultValue = "1") int page,
                              @RequestParam(value = "limit", defaultValue = "10") int limit){
        //开启分页
        PageHelper.startPage(page, limit, true);
        List<ProgramType> list = programTypeService.findAll();
        if (CollUtil.isNotEmpty(list)){
            PageInfo<ProgramType> pageInfo = new PageInfo<>(list);
            return ReturnData.ok(list);
        }
        return ReturnData.error(ReturnConstant.FAIL,"查询节目类型信息失败,请重新查询");
    }

    /**
     * 根据参数查询节目类型信息
     * @param param 参数
     *        id 活动节目id
     *        programTypeName 节目类型名称
     *        programTypeState 节目类型状态
     * @return 节目类型信息
     */
    @RequestMapping("/findProgramTypeByParam")
    public ReturnData findProgramTypeByParam(@RequestParam(value = "page", defaultValue = "1") int page,
                                             @RequestParam(value = "limit", defaultValue = "10") int limit,
                                             @RequestBody Map<String,Object> param){
        PageHelper.startPage(page, limit, true);
        List<ProgramType> list = programTypeService.findProgramTypeByParam(param);
        if (CollUtil.isNotEmpty(list)){
            PageInfo<ProgramType> pageInfo = new PageInfo<>(list);
            return ReturnData.ok(list);
        }
        return ReturnData.error(ReturnConstant.FAIL,"查询节目类型信息失败,请重新查询");
    }

    /**
     * 根据id查询节目类型信息
     * @param programTypeId 节目类型id
     * @return 节目类型信息
     */
    @RequestMapping("/findContentById")
    public ReturnData findProgramTypeById(@RequestParam(value = "programTypeId") Integer programTypeId){
        ProgramType programType = programTypeService.findProgramTypeById(programTypeId);
        if (StringUtils.isNotNull(programType)) {
            return ReturnData.ok(programType);
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "查询节目类型信息失败,请重新查询");
        }
    }

    /**
     * 添加节目类型信息
     * @param programType 节目类型信息
     * @return 是否成功
     */
    public ReturnData addProgramType(@RequestBody ProgramType programType){
        Integer i = programTypeService.addProgramType(programType);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "活动节目信息添加错误");
        }
    }

    /**
     * 修改节目类型信息
     * @param programType 节目类型信息
     * @return 是否成功
     */
    public ReturnData exitProgramType(@RequestBody ProgramType programType){
        Integer i = programTypeService.exitProgramType(programType);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "活动节目信息修改错误");
        }
    }

    /**
     * 根据节目id删除节目类型信息
     * @param programTypeId 节目类型id
     * @return 是否成功
     */
    public ReturnData delProgramType(@RequestParam(value = "programTypeId") Integer programTypeId){
        Integer i = programTypeService.delProgramType(programTypeId);
        if (StringUtils.isNotNull(i)) {
            return ReturnData.ok();
        } else {
            return ReturnData.error(ReturnConstant.FAIL, "活动节目信息删除错误");
        }
    }
}
